package com.tiansu.reborn.wanda.modules.executor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiansu.reborn.wanda.modules.executor.dao.*;
import com.tiansu.reborn.wanda.modules.executor.entity.*;
import com.tiansu.reborn.wanda.modules.executor.entity.enums.SampleEnum;
import com.tiansu.reborn.wanda.modules.executor.entity.result.SampleValueResult;
import com.tiansu.reborn.wanda.modules.executor.utils.OkHttpUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TodoController {


    @Autowired
    private ToDoDao toDoDao;

    @Autowired
    private SampleHourDao sampleHourDao;

    @Autowired
    private PayRecordDao payRecordDao;


    @Autowired
    private AccountDao accountDao;


    @Autowired
    private RoomDao roomDao;


    @Value("${my.todourl}")
    private String todoUrl;


    @GetMapping("/9527")
    public String index() {

        return "9527";
    }

    @PostMapping("/todo_submit")
    @ResponseBody
    public SampleValueResult todo_submit(String detailsId) {

        SampleValueResult result = new SampleValueResult();
        TodoEntity todoEntity = getByDetailsId(detailsId);
        SampleHourEntity tableLocation = sampleHourDao.getTableLocation(todoEntity.getStayobj(), SampleEnum.BALANCE.getData());

        List<SampleValueEntity> sampleValue = sampleHourDao.getSampleValue(tableLocation.getSampletablename(), tableLocation.getColumnname(), todoEntity.getUpdatetime());
        result.setMoney(todoEntity.getParam());
        result.setUpdateTime(new DateTime(todoEntity.getUpdatetime()).toString("yyyy-MM-dd HH:mm:ss"));
        result.setList(sampleValue);


        SampleHourEntity tableLocation1 = sampleHourDao.getTableLocation(todoEntity.getStayobj(), SampleEnum.OVERDRAFT.getData());
        List<SampleValueEntity> sampleValue1 = sampleHourDao.getSampleValue(tableLocation1.getSampletablename(), tableLocation1.getColumnname(), todoEntity.getUpdatetime());
        result.setList_1(sampleValue1);



        List<PayRecordEntity> payRecords = payRecordDao.getPayRecords(todoEntity.getStayobj(), todoEntity.getBrandid(), todoEntity.getUpdatetime());
        result.setList1(payRecords);
        return result;


    }

    private TodoEntity getByDetailsId(String detailsId) {
        return toDoDao.selectOne(new QueryWrapper<TodoEntity>().lambda().eq(TodoEntity::getDetailsid, detailsId));
    }

    @PostMapping("/todo_retry")
    @ResponseBody
    public Boolean todo_retry(String detailsId) {
        TodoEntity todoEntity = getByDetailsId(detailsId);
        if(!todoEntity.getExmsg().equals("9527")){
            return false;
        }
        TodoEntity updateEntity = new TodoEntity();
        updateEntity.setId(todoEntity.getId());
        updateEntity.setIsok(0);
        updateEntity.setExmsg("");
        //只更新 isok 跟exmsg
        toDoDao.updateById(updateEntity);

        Map<String, String> map = new HashMap<>(1);
        // 一次只处理一条
        map.put("Inputs", todoEntity.getId().toString());
        String result = OkHttpUtil.postFormParams(todoUrl, map);
        return true;
    }

    @PostMapping("/todo_insert_payrecord")
    @ResponseBody
    public Boolean todo_insert_payrecord(String detailsId) {
        TodoEntity todoEntity = getByDetailsId(detailsId);
        TodoEntity updateEntity = new TodoEntity();
        
        if(!todoEntity.getExmsg().equals("9527")){
            return false;
        }

        
        updateEntity.setId(todoEntity.getId());
        updateEntity.setIsok(1);
        updateEntity.setExmsg("");
        //只更新 isok 跟exmsg
        toDoDao.updateById(updateEntity);

        AccountEntity accountEntity = accountDao.selectById(todoEntity.getBrandid());
        RoomEntity roomEntity = roomDao.selectById(accountEntity.getPlazaid());
        SampleHourEntity tableLocation = sampleHourDao.getTableLocation(todoEntity.getStayobj(), SampleEnum.BALANCE.getData());
        SampleValueEntity sampleValue = sampleHourDao.getLatelyValue(tableLocation.getSampletablename(), tableLocation.getColumnname(), todoEntity.getUpdatetime());
        if (sampleValue == null) {
            sampleValue = new SampleValueEntity();
        }

        Integer selectCount = payRecordDao.selectCount(new QueryWrapper<PayRecordEntity>().lambda().eq(PayRecordEntity::getBillserial, detailsId));
        if (selectCount>0){
            return false;
        }

        //插入充值记录
        PayRecordEntity payRecordEntity = new PayRecordEntity();
        Integer paytype = getPaytype(detailsId);

        payRecordEntity.setOperationtime(todoEntity.getUpdatetime());
        payRecordEntity.setAccountcode(accountEntity.getCname());
        payRecordEntity.setRoomname(roomEntity.getCname());
        payRecordEntity.setDeviceid(todoEntity.getStayobj());
        payRecordEntity.setPaycount(todoEntity.getParam());
        payRecordEntity.setOperater(todoEntity.getOperuser());
        payRecordEntity.setComputername("BJS0-172-V1");
        payRecordEntity.setPaptaker(paytype);
        payRecordEntity.setBillserial(detailsId);
        payRecordEntity.setRechargetype(paytype);
        if (sampleValue.getSamplevalue()==null){
            sampleValue.setSamplevalue(new Double(0));
        }
        payRecordEntity.setBeforerecharge(sampleValue.getSamplevalue());
        payRecordEntity.setAftererecharge(sampleValue.getSamplevalue() + todoEntity.getParam());
        payRecordEntity.setRechargetime(new Date());

        PayRecordEntity lastPayRecord = payRecordDao.getLastPayRecord(todoEntity.getStayobj());
        int count=0;
        if (null != lastPayRecord){
            count=lastPayRecord.getCount();
        }

        payRecordEntity.setCount(count+1);
        payRecordEntity.setUpmark(0);
        payRecordEntity.setBrandid(todoEntity.getBrandid());
        payRecordEntity.setPlazaid(roomEntity.getId());
        payRecordEntity.setBunkcode(accountEntity.getNickname());
        payRecordDao.insert(payRecordEntity);
        return true;
    }

    private Integer getPaytype(String detailsId) {
        if (detailsId.startsWith("SHD")) {

            return 0;
        }
        if (detailsId.startsWith("ZN")) {
            return 1;
        }
        if (detailsId.startsWith("YJ")) {
            return 2;
        }
        if (detailsId.startsWith("BC")) {
            return 4;
        }
        if (detailsId.startsWith("CW")) {
            return 5;
        }
        return 0;

    }


}
